function ajax(opt) {
    opt = opt || {};
    opt.method = opt.method || 'GET';
    opt.url = opt.url || '';
    opt.async = opt.async || true;
    opt.data = opt.data || null;
    opt.raw = opt.raw || false;
    opt.success = opt.success || function () { };
    opt.error = opt.error || function () { };
    var xmlHttp = null;
    if (XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    else {
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
    var params = [];
    for (var key in opt.data) {
        params.push(key + '=' + opt.data[key]);
    }
    var postData = params.join('&');
    var attch = 'rd=' + Date.now();
    if (postData) {
        attch = postData + '&rd=' + Date.now();
    }
    var meth = opt.method.toUpperCase();
    if (meth === 'POST') {
        xmlHttp.open(meth, opt.url, opt.async);
        if (opt.headers && Object.prototype.toString.call(opt.headers)==='[object Object]') {
            for (var customHead in opt.headers) { 
                xmlHttp.setRequestHeader(customHead,opt['headers'][customHead])
            }
        } else { 
            xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
            //xmlHttp.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
        }
        
        if (opt.raw) {
            xmlHttp.send(opt.data);
        } else { 
            xmlHttp.send(postData);
        }
        
    } else {
        if (opt.url.indexOf('?') > -1) {
            opt.url = opt.url + '&' + attch;
        } else {
            opt.url = opt.url + '?' + attch;
        }
        xmlHttp.open(meth, opt.url, opt.async);
        xmlHttp.send(null);
    }
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
            if ((xmlHttp.status >= 200 && xmlHttp.status < 300) || xmlHttp.status == 304) {
                //xmlHttp.responseText无论是[],{},'',null得到的typeof都是string
                var data = xmlHttp.responseText;
                data = data.replace(/^\s*/, '')
                if (data[0] === '{') {
                    data = JSON.parse(data);
                }
                opt.success(data);
            } else {
                opt.error(xmlHttp.statusText, xmlHttp.status);
            }
        }
    };
    return xmlHttp;
}


function ajaxFD(opt) {
    opt.method = 'POST';
    opt.url = opt.url || '';
    opt.async = opt.async || true;
    opt.data = opt.data || null;
    opt.success = opt.success || function () { };
    opt.error = opt.error || function () { };
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open(opt.method, opt.url, opt.async);
    //使用FormData时,让浏览器自行设置Content-Type,无需自己指定multipart/form-data
    xmlHttp.send(opt.data);
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
            if ((xmlHttp.status >= 200 && xmlHttp.status < 300) || xmlHttp.status == 304) {
                var data = xmlHttp.responseText;
                if (data[0] === '{') {
                    data = JSON.parse(data);
                }
                opt.success(data);
            } else {
                opt.error(xmlHttp.statusText, xmlHttp.status);
            }
        }
    };
    return xmlHttp;
}
